package com.itheima.reggie.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.commom.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

//套餐
@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    @Override
    public Page<Setmeal> findByPage(Integer pageNum, Integer pageSize, String name) {
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.like(StrUtil.isAllNotBlank(name), Setmeal::getName, name);
        //分页
        Page<Setmeal> page = new Page<>(pageNum, pageSize);
        page = setmealMapper.selectPage(page, qw);
        for (Setmeal setmeal : page.getRecords()) {
            Category category = categoryMapper.selectById(setmeal.getCategoryId());
            setmeal.setCategoryName(category.getName());
            //根据id查询并封装菜品列表
            LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
            List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper);
            setmeal.setSetmealDishes(setmealDishList);
        }
        return page;





    }

    @Override
    public void sacve(Setmeal setmeal) {
        setmealMapper.insert(setmeal);
        List<SetmealDish> setmealDishList = setmeal.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishList) {
            setmealDish.setSetmealId(setmeal.getId());
            setmealDishMapper.insert(setmealDish);
        }
    }

    @Override
    public Setmeal findById(Long id) {
        Setmeal setmeal=setmealMapper.selectById(id);
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish>setmealDishes=setmealDishMapper.selectList(queryWrapper);
        setmeal.setSetmealDishes(setmealDishes);
        return setmeal;
    }

    @Override
    public void updateSetmeal(Setmeal setmeal) {
        //修改基本信息
        setmealMapper.updateById(setmeal);
        //删除旧菜品
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId,setmeal.getId());
        setmealDishMapper.delete(qw);
        //添加新菜品
        for (SetmealDish setmealDish : setmeal.getSetmealDishes()) {
            //关联id
            setmealDish.setSetmealId(setmeal.getId());

            //添加到数据库
            setmealDishMapper.insert(setmealDish);
        }
    }

    @Override
    public void updateStatusSetmeal(Integer status, List<Long> ids) {
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.in(Setmeal::getId,ids);

        //构建实体类
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);

        //修改状态
        setmealMapper.update(setmeal,qw);
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.in(Setmeal::getId,ids); // id in(1,3,5);
        qw.eq(Setmeal::getStatus,1); //   status=1
        Integer count = setmealMapper.selectCount(qw);
        if (count>0) {
            throw new CustomException("套餐中有启售状态，请先停售再删除~~~");
        }

        // 2.删套餐
        setmealMapper.deleteBatchIds(ids);

        // 3.删除套餐内菜品中间表  delete from setmeal_dish where setmeal_id in(1,2,3)
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SetmealDish::getSetmealId,ids);
        setmealDishMapper.delete(wrapper);
    }

    @Override
    public List<Setmeal> list(Long categoryId, Integer status) {
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(Setmeal::getCategoryId,categoryId);
        qw.eq(Setmeal::getStatus,status);
        return setmealMapper.selectList(qw);
    }



    @Override
    public List<Dish> findDishDetail(Long id) {
        // 1.查询套餐菜品中间表集合
        // 1-1 构建条件  select * from setmeal_dish where setmeal_id = ?
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId, id);
        // 1-2 查询
        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(qw);
        // 2.遍历
        List<Dish> dishList = new ArrayList<>();
        for (SetmealDish setmealDish : setmealDishList) {
            // 2-1 查询菜品
            Dish dish = dishMapper.selectById(setmealDish.getDishId());
            // 2-2 添加套餐中数量
            dish.setCopies(setmealDish.getCopies());
            // 2-3 添加返回菜品集合
            dishList.add(dish);
        }
        // 3.返回结果
        return dishList;

    }
}
